//
//  TestImagePickVC.swift
//  GHSwiftProject
//
//  Created by Guanghui Liao on 4/12/18.
//  Copyright © 2018 liaoworking. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift
import AVFoundation
import Photos
class TestImagePickVC: GHBaseTableViewController {

    let disposeBag = DisposeBag()
    var OperationBlock:(()->())?
    var imagePicker: UIImagePickerController!

    
    override func viewDidLoad() {
        super.viewDidLoad()

        configTestBtn()
        configImagePicker()
    }

    func configTestBtn() {
        let testBtn = UIButton(frame: CGRect(x: 40, y: 100, width: 80, height: 30))
        testBtn.setTitle("TESTTakePhoto", for: .normal)
        testBtn.backgroundColor = UIColor.red
        testBtn.rx.tap.subscribe(onNext:{
            self.chooiceTakePhoto()
        }).disposed(by: disposeBag)
        view.addSubview(testBtn)
        
        let testBtn1 = UIButton(frame: CGRect(x: 40, y: 200, width: 80, height: 30))
        testBtn1.setTitle("TESTPickPhoto", for: .normal)
        testBtn1.backgroundColor = UIColor.red
        testBtn1.rx.tap.subscribe(onNext:{
            self.pickPhotos()
        }).disposed(by: disposeBag)
        view.addSubview(testBtn1)
    }
    
    func chooiceTakePhoto() {
        let alertSheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
        
        let takePhotoAlert = UIAlertAction(title: "拍照", style: UIAlertActionStyle.default) { (action) in
            self.takePhotos()

        }
        
        let pickPhotoAlert = UIAlertAction(title: "从相册选取", style: UIAlertActionStyle.default) { (action) in
            self.pickPhotos()
        }
        let cancleAlert = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel) { (action) in
        }
        
        alertSheet.addAction(takePhotoAlert)
        alertSheet.addAction(pickPhotoAlert)
        alertSheet.addAction(cancleAlert)
        present(alertSheet, animated: true, completion: nil)

    }
    
    
    func configImagePicker() {
        DispatchQueue.main.async {
            self.imagePicker = UIImagePickerController()
            self.imagePicker.delegate = self
            self.imagePicker.allowsEditing = true
            self.imagePicker.view.backgroundColor = WHITE_COLOR
        }
    }

    @objc func pickPhotos()  {
        imagePicker.sourceType = .photoLibrary
        
        let status = PHPhotoLibrary.authorizationStatus()
        
        if status == PHAuthorizationStatus.restricted || status == PHAuthorizationStatus.denied {
           let alertVC = UIAlertController.init(title: "提示", message: "尚未打开相册权限，请前往设置中进行开启", defaultActionButtonTitle: "确定", tintColor: UIColor.black)
            present(alertVC, animated: true, completion: nil)
        }else{
                self.present(imagePicker, animated: true, completion: nil)
        }
    }
    
   @objc func takePhotos() {
    imagePicker.sourceType = .camera
    
    let authStatus:AVAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
    
    if(authStatus == AVAuthorizationStatus.denied || authStatus == AVAuthorizationStatus.restricted) {
        let alertVC = UIAlertController.init(title: "提示", message: "尚未打开相机权限，请前往设置中进行开启", defaultActionButtonTitle: "确定", tintColor: UIColor.black)
        present(alertVC, animated: true, completion: nil)
    }else {
        self.present(imagePicker, animated: true, completion: nil)
    }

    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}


extension TestImagePickVC:UIImagePickerControllerDelegate, UINavigationControllerDelegate{
 
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        
        let image = info["UIImagePickerControllerOriginalImage"] as! UIImage
        print(image)
        
        picker.dismiss(animated: true, completion: nil)
        
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
    
}
 
 import UIKit
 
 var  width = UIScreen.main.bounds.width
 var  heigth = UIScreen.main.bounds.height
 
 class YMImgPickerVC: UIViewController {
 
 
 var bgView : UIView?
 var cancelBtn : UIButton?
 var photoBtn : UIButton?
 var cameraBtn : UIButton?
 var callbackWithImg : ((_ imgData:Data) -> Void)? = nil
 
 
 var  imagePickerCtr : UIImagePickerController!
 
 
 
 
 override func viewDidLoad() {
 super.viewDidLoad()
 setupUI()
 initImagePickerCtr()
 
 
 }
 
 override func didReceiveMemoryWarning() {
 super.didReceiveMemoryWarning()
 
 
 }
 
 
 }
 
 
 
 //设置界面
 extension YMImgPickerVC {
 
 fileprivate func setupUI(){
 //背景View
 bgView = UIView(frame: CGRect(x: 0, y: UIScreen.main.bounds.height - 127, width: UIScreen.main.bounds.width, height: 127))
 bgView?.backgroundColor = UIColor.gray
 guard let bgview1 = bgView else {
 return ;
 }
 view.addSubview(bgview1)
 
 //取消按钮
 cancelBtn = UIButton(frame: CGRect(x: 0, y: 87, width: UIScreen.main.bounds.width, height: 40))
 cancelBtn?.setTitle("取消", for: .normal)
 cancelBtn?.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
 cancelBtn?.setTitleColor(UIColor.black, for: .normal)
 cancelBtn?.tag = 10001
 cancelBtn?.backgroundColor = UIColor.white
 guard let btn = cancelBtn else {
 return ;
 }
 bgView?.addSubview(btn)
 
 
 //相册选择按钮
 photoBtn = UIButton(frame: CGRect(x: 0, y: 41, width: UIScreen.main.bounds.width, height: 40))
 photoBtn?.setTitle("从相册选择", for: .normal)
 photoBtn?.setTitleColor(UIColor.black, for: .normal)
 photoBtn?.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
 photoBtn?.tag = 10002
 photoBtn?.backgroundColor = UIColor.white
 guard let btn1 = photoBtn else {
 return ;
 }
 bgView?.addSubview(btn1)
 
 
 //照相按钮
 cameraBtn = UIButton(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 40))
 cameraBtn?.setTitle("拍照", for: .normal)
 cameraBtn?.setTitleColor(UIColor.black, for: .normal)
 cameraBtn?.tag = 10003
 cameraBtn?.backgroundColor = UIColor.white
 cameraBtn?.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
 guard let btn2 = cameraBtn else {
 return ;
 }
 bgView?.addSubview(btn2)
 
 }
 
 fileprivate func initImagePickerCtr(){
 
 imagePickerCtr = UIImagePickerController()
 imagePickerCtr.delegate = self
 //设置是否可以管理已经存在的图片或视频
 imagePickerCtr.allowsEditing = true
 
 
 
 }
 
 
 }
 
 
 
 
 //按钮带点击的方法
 extension YMImgPickerVC {
 
 
 @objc fileprivate func btnClick(btn : UIButton){
 switch btn.tag {
 
 case 10001:
 self.dismiss(animated: true, completion: {
 
 })
 
 
 
 case 10002:
 //相册
 chooseImg(type: .photoLibrary)
 
 
 case 10003:
 //相机
 chooseImg(type: .camera)
 default:
 break
 }
 
 }
 
 
 
 //相册选择或拍照
 func chooseImg(type:UIImagePickerControllerSourceType){
 
 imagePickerCtr.sourceType = type
 if UIImagePickerController.isSourceTypeAvailable(type){
 self.present(imagePickerCtr, animated: true, completion: {
 
 })
 
 }else{
    
 }
 
 
 }
 
 }
 
 
 
 //MARK: - UIImagePickerControllerDelegate
 extension YMImgPickerVC : UIImagePickerControllerDelegate, UINavigationControllerDelegate{
 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
 
 let  img = info[UIImagePickerControllerOriginalImage] as? UIImage
 
 
 
 //压缩图片
 //默认压缩到100KB
 let  imgData = compressImage(image: img ?? UIImage(), maxLength: 1024*100) //  UIImageJPEGRepresentation(img!, 0.5)
 picker.dismiss(animated: true, completion: {
 
 guard let data = imgData else {
 return ;
 }
 self.callbackWithImg!(data)
 
 })
 self.dismiss(animated: true) {
 
 }
 
 }
 
 func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
 picker.dismiss(animated: true) {
 
 }
 self.dismiss(animated: true) {
 
 }
 }
 
 
 
 }
 
 
 
 //指定压缩图片大小
 extension YMImgPickerVC {
 
 
 
 /// 预缩图片到指定字节
 ///
 /// - Parameters:
 ///   - image: 需要压缩的图片
 ///   - maxLength: 压缩的大小
 /// - Returns: 返回压缩的图片
 fileprivate func compressImage(image : UIImage, maxLength: Int ) -> Data? {
 
 var compress:CGFloat = 0.9
 var data = UIImageJPEGRepresentation(image, compress)
 while (data?.count)! > maxLength && compress > 0.001 {
 compress -= 0.02
 data = UIImageJPEGRepresentation(image, compress)
 }
 return data
 }
 
 }

